package com.example.wechatbusiness.service.user.impl;

import com.example.wechatbusiness.config.Result;
import com.example.wechatbusiness.mapper.MenuMapper;
import com.example.wechatbusiness.mapper.UserMapper;
import com.example.wechatbusiness.pojo.Menu;
import com.example.wechatbusiness.pojo.User;
import com.example.wechatbusiness.pojo.UserExample;
import com.example.wechatbusiness.service.user.AdminService;
import com.example.wechatbusiness.utils.MD5;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminSericeImpl implements AdminService {
    @Resource
    UserMapper userMapper;
    @Resource
    MenuMapper menuMapper;

    @Override
    public Result loginAdmin(String userName, String password) {
        password = MD5.MD5(password);
        UserExample userExample = new UserExample();
        userExample.createCriteria().andPhoneEqualTo(userName).andPasswordEqualTo(password).andStatusEqualTo(1);
        List<User> users = userMapper.selectByExample(userExample);
        if (users != null && users.size() > 0) {
            Integer roleId = users.get(0).getRoleid();
            Integer teamId = users.get(0).getTeamid();

            List<Menu> menus = menuMapper.selectByTeamIdAndRoleId(teamId, roleId);
            Map<Integer, List<Menu>> map = new HashMap<>();
            for (Menu menu : menus) {//菜单需要一级菜单
                if (menu.getOneid() == 0) {
                    List<Menu> list = new ArrayList<>();
                    list.add(menu);
                    map.put(menu.getId(), list);
                    continue;
                }
                List<Menu> list = map.get(menu.getOneid());
                list.add(menu);
                map.put(menu.getOneid(), list);
            }
            List<List> list = new ArrayList<>();
            for (Integer integer : map.keySet()) {
                list.add(map.get(integer));
            }
            return Result.ok(200, "登录成功", list);
        } else {
            return Result.ok(100, "登录失败", null);
        }
    }
}
